package hoemWork;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class HoemWork {
	String name;

	public HoemWork(String name) {
		this.name = name;
	}

	public static void main(String[] args) {

		File oldfile = new File("E:/FileTest/test/Hello.txt");
		File newfile = new File("E:/FileTest1");   //只能传文件夹
		findAndCopyFile(oldfile, newfile);

	}

	public static void findAndCopyFile(File oldfile, File newfile) {
		
		if (!newfile.exists()) {
			newfile.mkdirs();		//不存在时 ，创建文件夹
		}
		if (oldfile.isFile()) {
			copy(oldfile, newfile);		//当拷贝的是文件时，直接拷贝
		} else {
			for (File file : oldfile.listFiles()) {		//循环拷贝文件夹
				if (!file.isHidden()) {
					if (file.isDirectory()) {
						findAndCopyFile(file, new File(newfile + "/" + file.getName()));
					} else {
						copy(file, newfile);
					}
				}
			}
		}
	}

	private static void copy(File file, File newfile) {
		BufferedReader duq = null;
		PrintWriter xieru = null;
		try {
			duq = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			xieru = new PrintWriter(newfile + "/" + file.getName());
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		String line = null;
		try {
			while ((line = duq.readLine()) != null) {
				xieru.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (duq != null)
					duq.close();
				if (xieru != null)
					xieru.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
